
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CrossCursorApp
{
    public partial class CrossCursorForm : Form
    {
        private PictureBox pictureBox;
        private Point cursorPosition;
        private bool isMoving;
        private Point lastMousePosition;
        private Bitmap originalImage;
        private float zoomFactor = 1.0f;
        private Point imageOffset = new Point(0, 0);

        public CrossCursorForm()
        {
            InitializeComponent();
            InitializePictureBox();
        }

        private void InitializePictureBox()
        {
            // 创建PictureBox控件
            pictureBox = new PictureBox
            {
                Location = new Point(20, 20),
                Size = new Size(600, 400),
                BorderStyle = BorderStyle.FixedSingle,
                BackColor = Color.White,
                SizeMode = PictureBoxSizeMode.Zoom
            };

            // 注册事件
            pictureBox.Paint += PictureBox_Paint;
            pictureBox.MouseDown += PictureBox_MouseDown;
            pictureBox.MouseMove += PictureBox_MouseMove;
            pictureBox.MouseUp += PictureBox_MouseUp;
            pictureBox.MouseWheel += PictureBox_MouseWheel;

            this.Controls.Add(pictureBox);

            // 创建控制按钮
            CreateControlButtons();
        }

        private void CreateControlButtons()
        {
            Button btnLoad = new Button
            {
                Text = "加载图片",
                Location = new Point(630, 20),
                Size = new Size(100, 30)
            };
            btnLoad.Click += BtnLoad_Click;

            Button btnReset = new Button
            {
                Text = "重置视图",
                Location = new Point(630, 60),
                Size = new Size(100, 30)
            };
            btnReset.Click += BtnReset_Click;

            this.Controls.Add(btnLoad);
            this.Controls.Add(btnReset);
        }

        private void PictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (pictureBox.Image == null) return;

            // 绘制十字光标
            DrawCrossCursor(e.Graphics);
        }

        private void DrawCrossCursor(Graphics g)
        {
            // 设置画笔属性
            using (Pen crossPen = new Pen(Color.Red, 2))
            {
                // 绘制水平线
                g.DrawLine(crossPen, 0, cursorPosition.Y, pictureBox.Width, cursorPosition.Y);
                
                // 绘制垂直线
                g.DrawLine(crossPen, cursorPosition.X, 0, cursorPosition.X, pictureBox.Height);
                
                // 绘制中心点
                using (Brush centerBrush = new SolidBrush(Color.Blue))
                {
                    g.FillEllipse(centerBrush, cursorPosition.X - 3, cursorPosition.Y - 3, 6, 6);
                }
            }
        }

        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMoving = true;
                lastMousePosition = e.Location;
                cursorPosition = e.Location;
                pictureBox.Invalidate(); // 触发重绘
            }
        }

        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {
                // 更新光标位置
                cursorPosition = e.Location;
                pictureBox.Invalidate();
            }
        }

        private void PictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            isMoving = false;
        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            // 缩放功能
            if (pictureBox.Image != null)
            {
                float zoomChange = e.Delta > 0 ? 1.1f : 0.9f;
                zoomFactor *= zoomChange;
                
                // 限制缩放范围
                zoomFactor = Math.Max(0.1f, Math.Min(5.0f, zoomFactor));
                
                pictureBox.Invalidate();
            }
        }

        private void BtnLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        originalImage = new Bitmap(openFileDialog.FileName);
                        pictureBox.Image = originalImage;
                        cursorPosition = new Point(pictureBox.Width / 2, pictureBox.Height / 2);
                        pictureBox.Invalidate();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"加载图片失败: {ex.Message}", "错误", 
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            zoomFactor = 1.0f;
            imageOffset = new Point(0, 0);
            cursorPosition = new Point(pictureBox.Width / 2, pictureBox.Height / 2);
            pictureBox.Invalidate();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new CrossCursorForm());
        }
    }
}
